package cn.kgc.javaweb.service.impl;

import cn.kgc.javaweb.dao.UserDao;
import cn.kgc.javaweb.dao.impl.UserDaoImpl;
import cn.kgc.javaweb.entity.User;
import cn.kgc.javaweb.service.UserService;
import cn.kgc.javaweb.utils.PageData;
import cn.kgc.javaweb.utils.ResponseResult;

import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/9/8
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    @Override
    public Boolean selectByUsername(String username) {
        User user = userDao.selectByUsername(username);
        return user == null;
    }

    @Override
    public void register(String username, String password) {

        userDao.register(username, password);


    }

    @Override
    public User selectByUsernameAndPassword(String username, String password) {
        return userDao.selectByUsernameAndPassword(username, password);
    }

    @Override
    public ResponseResult selectAll() {

        // 查询所有的用户信息
        List<User> users = userDao.selectAll();
        ResponseResult responseResult = new ResponseResult(users);

        return responseResult;
    }

    @Override
    public void add(User user) {
        Integer line = userDao.add(user);
    }

    @Override
    public void delete(int id) {
        Integer line = userDao.delete(id);
    }

    @Override
    public User selectUpdateUser(String username) {
        User user = userDao.selectByUsername(username);
        return user;
    }

    @Override
    public void updateUser(User user) {

        Integer line = userDao.updateUser(user);

    }

    @Override
    public ResponseResult selectByPage(Integer page, Integer size) {

        //计算分页查询数据的其实位置
        Integer start = (page - 1) * size;
        List<User> users = userDao.selectByPage(start, size);

        // 封装分页数据
        PageData pageData = new PageData();
        pageData.setCurrentPage(page);
        pageData.setSize(size);
        // 设置总条数
        List<User> usrs = userDao.selectAll();
        pageData.setTotal(usrs.size());
        // 当前页展示的数据
        pageData.setData(users);

        // 将分页数据 添加到响应对象中进行数据响应
        return new ResponseResult(pageData );
    }

    @Override
    public void updateImg(Integer userId, String lastName) {
        Integer line =   userDao.updateImg(userId,lastName);
    }
}
